﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFP;
using SFP.CollisionDetection;

namespace HamsterDelivery.LevelStructure.Reactions
{
    public class ObstacleReaction : GlobalReaction
    {
        private HamsterGame game;

        public ObstacleReaction(HamsterGame game)
        {
            this.game = game;
        }

        protected override void fromBeneath(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = ReactionUtils.collisionMaxOverlap(collisionInfos);
            Player player = game.Player;
            Level level = game.Level;

            player.VelocityY = 0;
            player.Jump = false;

            if (level.LayerManager.isYAtBeginn() || level.LayerManager.isYAtEnd())
                player.Y += collisionInfo.Overlap;
            else
                level.LayerManager.scrollY(-collisionInfo.Overlap);
        }

        protected override void fromAbove(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = ReactionUtils.collisionMaxOverlap(collisionInfos);
            Player player = game.Player;
            Level level = game.Level;

            player.GroundCollisionFound = true;
            player.OnGround = true;
            player.Jump = false;

            if (level.LayerManager.isYAtBeginn() || level.LayerManager.isYAtEnd())
                player.Y -= collisionInfo.Overlap;
            else
                level.LayerManager.scrollY(collisionInfo.Overlap);
        }

        protected override void fromLeft(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = ReactionUtils.collisionMaxOverlap(collisionInfos);
            Player player = game.Player;
            Level level = game.Level;

            player.VelocityX = 0;
            if (level.LayerManager.isXAtBeginn() || level.LayerManager.isXAtEnd())
                player.X -= collisionInfo.Overlap;
            else
                level.LayerManager.scrollX(-collisionInfo.Overlap);
        }

        protected override void fromRight(HashSet<CollisionInfo> collisionInfos)
        {
            CollisionInfo collisionInfo = ReactionUtils.collisionMaxOverlap(collisionInfos);
            Player player = game.Player;
            Level level = game.Level;

            player.VelocityX = 0;
            if (level.LayerManager.isXAtBeginn() || level.LayerManager.isXAtEnd())
                player.X += collisionInfo.Overlap;
            else
                level.LayerManager.scrollX(collisionInfo.Overlap);
        }

        protected override void touchesTop(HashSet<CollisionInfo> collisionInfos)
        {
            Player player = game.Player;
            Level level = game.Level;

            player.GroundCollisionFound = true;
            player.OnGround = true;
            player.Jump = false;
        }

        protected override void touchesBottom(HashSet<CollisionInfo> collisionInfos)
        {
        }

        protected override void touchesLeft(HashSet<CollisionInfo> collisionInfos)
        {
        }

        protected override void touchesRight(HashSet<CollisionInfo> collisionInfos)
        {
        }
    }
}
